﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels.IndexViewModels
{
    public static class RelatorioVisitaIndexColumns
    {
        public static Guid Data = new Guid("9604742f-8b49-43d0-863a-c54fbcc4ab7d");
        public static Guid Cliente = new Guid("92036d4f-5fdd-4853-8826-4bc5b986f6eb");
        public static Guid Vendedor = new Guid("1dbb2ecc-142e-4000-9d25-a923e90d8e69");
        public static Guid Observacao = new Guid("71d7a640-aa78-4b53-9e35-bf14084cc810");
    }

    public class RelatorioVisitaIndexViewModel : AttributeHolderIndexViewModel<RelatorioVisita>
    {
        private readonly bool m_CanViewCliente;
        private readonly bool m_CanViewVendedor;

        public string VendedorBuscaValue { get; set; }
        public string ClienteBuscaValue { get; set; }
        public string ObservacaoBuscaValue { get; set; }
        public DateTime? DeBuscaValue { get; set; }
        public DateTime? AteBuscaValue { get; set; }

        public Dictionary<Guid, string> VendedorBusca { get; set; }

        public RelatorioVisitaIndexViewModel(bool canViewCliente, bool canViewVendedor) 
            : base("Relatórios de Visita")
        {
            m_CanViewCliente = canViewCliente;
            m_CanViewVendedor = canViewVendedor;
        }

        protected override IEnumerable<IField> GetSearchFields()
        {
            List<IField> result = new List<IField>
            {
                new DatePickerField("De", "de", DeBuscaValue),
                new DatePickerField("Até", "ate", AteBuscaValue),
                new TokenInputField("Vendedores", "vendedorIds", VendedorBuscaValue, "Search", "SearchVendedor", VendedorBusca),
                new AutoCompleteField("Clientes", "cliente", ClienteBuscaValue, "SearchCliente", "Search"),
            };
            result.AddRange(base.GetSearchFields());
            return result;
        }

        protected override void RegisterColumns(IndexColumnDefinitionsCollectionWrapper<RelatorioVisita> indexColumnDefinitionsCollectionWrapper)
        {
            indexColumnDefinitionsCollectionWrapper.Register(RelatorioVisitaIndexColumns.Data, (relatorioVisita, htmlHelper) => 
                htmlHelper.ActionLink(relatorioVisita.Data.ToShortDateString(), "Details", ControllerName, new { id = relatorioVisita.Id}, null));

            Func<RelatorioVisita, HtmlHelper, object> clienteTemplate = (relatorioVisita, htmlHelper) => m_CanViewCliente
                ? htmlHelper.ActionLink(relatorioVisita.ClienteNome, "Details", "Cliente", new { id = relatorioVisita.Cliente.Id }, null)
                : MvcHtmlString.Create(relatorioVisita.ClienteNome);
            indexColumnDefinitionsCollectionWrapper.Register(RelatorioVisitaIndexColumns.Cliente, clienteTemplate);
            Func<RelatorioVisita, HtmlHelper, object> vendedorTemplate = (relatorioVisita, htmlHelper) => m_CanViewVendedor
                ? htmlHelper.ActionLink(relatorioVisita.VendedorNome, "Details", "Vendedor", new { id = relatorioVisita.Vendedor.Id }, null)
                : MvcHtmlString.Create(relatorioVisita.VendedorNome);
            indexColumnDefinitionsCollectionWrapper.Register(RelatorioVisitaIndexColumns.Vendedor, vendedorTemplate);
            indexColumnDefinitionsCollectionWrapper.Register(RelatorioVisitaIndexColumns.Observacao, x => x.Observacao);
            base.RegisterColumns(indexColumnDefinitionsCollectionWrapper);
        }
    }
}